package com.yanqu.road.server.manger;

import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class CnActivityMgr extends TempMgr {

    private static Map<Integer, List<Integer>> activityServerShowMap;
    private static HashMap<Long, HashSet<String>> cnActivityUserMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    /**
     * 通过平台ID获取对应的服务器ID
     * @param platformUserId
     * @return
     */
    public static long getServerIdByPlatformUserId(String platformUserId) {
        for (Map.Entry<Long, HashSet<String>> entry : cnActivityUserMap.entrySet()) {
            long serverId = entry.getKey();
            if (entry.getValue().contains(platformUserId)) {
                return serverId;
            }
        }
        return 0;
    }

    /**
     * 是否是参与活动名单
     * @param platformUserId
     * @return
     */
    public static boolean isCnActivityUser(long serverId, String platformUserId) {
        if (cnActivityUserMap.containsKey(serverId) && cnActivityUserMap.get(serverId).contains(platformUserId)) {
            return true;
        }
        return false;
    }

    /**
     * 判断源渠道是否要展示目标渠道的区服列表(外部需要判断是否是活动服 做一层保险)
     * @param srcChannelId
     * @param destChannelId
     * @return
     */
    public static boolean isInDestChannelList(int srcChannelId, int destChannelId) {
        if (activityServerShowMap.containsKey(srcChannelId)) {
            for (int channelId : activityServerShowMap.get(srcChannelId)) {
                if (channelId == destChannelId) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        activityServerShowMap = ServerBussiness.getActivityServerShowMap();
        cnActivityUserMap = ServerBussiness.getCnActivityUserList();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
